$import( "SmitearWebGL.Objects.Parser.Collada.DaeEntity" );
$import( "SmitearWebGL.Objects.Parser.Collada.DaeTransform" );
$import( "SmitearWebGL.Objects.Parser.Collada.DaeInstanceController" );
$import( "SmitearWebGL.Objects.Parser.Collada.DaeInstanceGeometry" );
$import( "SmitearWebGL.Objects.Parser.Collada.DaeInstanceNode" );

SmitearWebGL.Objects.Parser.Collada.DaeNode = SmitearWebGL.Objects.Parser.Collada.DaeEntity.extend(
{
	/******** constructor ********/

	init : function( document, node, yUp )
	{
		/******** public property ********/

		this.type = 0;
		this.nodes = null;
		this.transforms = null;
		this.controllers = null;
		this.geometries = null;
		this.instance_nodes = null;
		this.instance_cameras = null;
		this.channels = null;
		this.hasMorphController = false;
		this.hasSkinController = false;

		/******** private property ********/

		this._yUp = yUp == undefined ? 1 : yUp;

		this._super( document, node );
		this._registClass( "SmitearWebGL.Objects.Parser.Collada.DaeNode" );
	}
	,
	read : function( node )
	{
		$THROW( node.localName == SmitearWebGL.Objects.Parser.Collada.Const.DAE_NODE_ELEMENT,
		"expected a '" + SmitearWebGL.Objects.Parser.Collada.Const.DAE_NODE_ELEMENT + "' element" );

		this.nodes = [];
		this.controllers = [];
		this.geometries = [];
		this.instance_nodes = [];
		this.instance_cameras = [];
		this.transforms = [];

		this._super( node );

		this.name = this.name && this.name.length ? this.name : this.id;
		this.type = this.getAttribute(node, SmitearWebGL.Objects.Parser.Collada.Const.DAE_TYPE_ATTRIBUTE) == "JOINT" ? SmitearWebGL.Objects.Parser.Collada.DaeNode.TYPE_JOINT : SmitearWebGL.Objects.Parser.Collada.DaeNode.TYPE_NODE;


		var children = node.childNodes;

		for( var i = 0; i < children.length; i++ )
		{
			var child = children[i];
			var floats;
			var csid = this.getAttribute( child, SmitearWebGL.Objects.Parser.Collada.Const.DAE_SID_ATTRIBUTE );
			var transform;

			switch( child.localName )
			{
				case SmitearWebGL.Objects.Parser.Collada.Const.DAE_ASSET_ELEMENT:
				break;

				case SmitearWebGL.Objects.Parser.Collada.Const.DAE_ROTATE_ELEMENT:
					floats = this.getFloats( child );
					transform = new SmitearWebGL.Objects.Parser.Collada.DaeTransform(SmitearWebGL.Objects.Parser.Collada.Const.DAE_ROTATE_ELEMENT, csid, floats);
					this.transforms.push( transform );
					break;

				case SmitearWebGL.Objects.Parser.Collada.Const.DAE_TRANSLATE_ELEMENT:
					floats = this.getFloats( child );
					transform = new SmitearWebGL.Objects.Parser.Collada.DaeTransform(SmitearWebGL.Objects.Parser.Collada.Const.DAE_TRANSLATE_ELEMENT, csid, floats);
					this.transforms.push( transform );
					break;

				case SmitearWebGL.Objects.Parser.Collada.Const.DAE_SCALE_ELEMENT:
					floats = this.getFloats( child );
					transform = new SmitearWebGL.Objects.Parser.Collada.DaeTransform(SmitearWebGL.Objects.Parser.Collada.Const.DAE_SCALE_ELEMENT, csid, floats);
					this.transforms.push( transform );
					break;

				case SmitearWebGL.Objects.Parser.Collada.Const.DAE_SKEW_ELEMENT:
					floats = this.getFloats( child );
					break;

				case SmitearWebGL.Objects.Parser.Collada.Const.DAE_LOOKAT_ELEMENT:
					floats = this.getFloats( child );
					break;

				case SmitearWebGL.Objects.Parser.Collada.Const.DAE_MATRIX_ELEMENT:
					floats = this.getFloats( child );
					transform = new SmitearWebGL.Objects.Parser.Collada.DaeTransform(SmitearWebGL.Objects.Parser.Collada.Const.DAE_MATRIX_ELEMENT, csid, floats);
					this.transforms.push( transform );
					break;

				case SmitearWebGL.Objects.Parser.Collada.Const.DAE_NODE_ELEMENT:
					this.nodes.push( new SmitearWebGL.Objects.Parser.Collada.DaeNode(this.document, child, this._yUp) );
					break;

				case SmitearWebGL.Objects.Parser.Collada.Const.DAE_INSTANCE_CAMERA_ELEMENT:
					this.instance_cameras.push( this.getAttribute( child, SmitearWebGL.Objects.Parser.Collada.Const.DAE_URL_ATTRIBUTE ) );
					break;

				case SmitearWebGL.Objects.Parser.Collada.Const.DAE_INSTANCE_CONTROLLER_ELEMENT:
					this.controllers.push( new SmitearWebGL.Objects.Parser.Collada.DaeInstanceController( this.document, child ) );
					break;

				case SmitearWebGL.Objects.Parser.Collada.Const.DAE_INSTANCE_GEOMETRY_ELEMENT:
					this.geometries.push( new SmitearWebGL.Objects.Parser.Collada.DaeInstanceGeometry( this.document, child ) );
					break;

				case SmitearWebGL.Objects.Parser.Collada.Const.DAE_INSTANCE_LIGHT_ELEMENT:
					break;

				case SmitearWebGL.Objects.Parser.Collada.Const.DAE_INSTANCE_NODE_ELEMENT:
					this.instance_nodes.push( new SmitearWebGL.Objects.Parser.Collada.DaeInstanceNode(this.document, child));
					break;

				case SmitearWebGL.Objects.Parser.Collada.Const.DAE_EXTRA_ELEMENT:
					break;

				default:
					break;
			}
		}

		for ( var i = 0; i < this.controllers.length; ++ i )
		{
			var controllerInstance = this.controllers[i];
			var controller = this.document.controllers[ controllerInstance.url ];
			if(controller && controller.morph)
			{
				this.hasMorphController = true;
			}
			else if(controller && controller.skin)
			{
				this.hasSkinController = true;
			}
		}
	}
}
);

SmitearWebGL.Objects.Parser.Collada.DaeNode.TYPE_NODE = 0;
SmitearWebGL.Objects.Parser.Collada.DaeNode.TYPE_JOINT = 1;